home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1997 July: Mac OS SDK / Dev.CD Jul 97 SDK2.toast / Development Kits (Disc 2) / ScriptX / Code Samples / untested / tcpip / ifish / wrapper.sx < prev   
Encoding:
Text File  |  1996-05-21  |  2.2 KB  |  86 lines  |  [TEXT/ttxt]

  1. -- Filename: 
  2. --     wrapper.sx
  3.  
  4. -- Other Files Required:
  5. --    None
  6.  
  7. -- Purpose:  
  8. --    Create a Controller subclass which wraps projectiles around in a space.
  9.  
  10. -- Specialized Classes:  
  11. --    None
  12.  
  13. -- Instructions to User: 
  14. --    Class Wrapper ensures that when objects move off of a space, they reappear
  15. --    on the other side.
  16.  
  17. -- Author:
  18. --     Steve Mayer
  19.  
  20. -- Class Wrapper is a Controller which controls Projectiles. The Wrapper checks
  21. -- its controllees at each tickle to see if they have moved outside of the space. If
  22. -- they have, they are automatically "wrapped" around to the other side.
  23.  
  24. in module InternetFish
  25.  
  26. class Wrapper (Controller)
  27. instance variables
  28.     state
  29.     client
  30. end
  31.  
  32. method init self {class Wrapper} #rest args #key state: client: ->
  33. (
  34.     apply nextMethod self targetCollection:(new Array) args    
  35.     append self.protocols Projectile
  36.     self.state := state
  37.     self.client := client
  38.     return self
  39. )
  40.  
  41. -- Method tickle is called by the parent space at each clock tick.
  42. -- It iterates through controllees and wraps them if they are outside the boundary.
  43.  
  44. method tickle self {class Wrapper} c ->
  45. (
  46.     if self.state = @standalone then (
  47.         local leftEdge
  48.         local topEdge
  49.         local rightEdge := self.space.boundary.x2
  50.         local bottomEdge := self.space.boundary.y2
  51.         -- Iterate through controllees
  52.         for n in self do
  53.         (
  54.             leftEdge := (self.space.boundary.x1 - n.width)
  55.             topEdge := (self.space.boundary.y1 - n.height)
  56.             -- Check if object is past any edges.
  57.             if (n.x < leftEdge) do (n.x := rightEdge)
  58.             if (n.x > rightEdge) do (n.x := leftEdge)
  59.             if (n.y < topEdge) do (n.y := bottomEdge)
  60.             if (n.y > bottomEdge) do (n.y := topEdge) 
  61.         )
  62.     ) else (
  63.         local rightEdge := self.space.boundary.x2
  64.         local bottomEdge := self.space.boundary.y2
  65.         -- Iterate through controllees
  66.         local n := self[1]
  67.         local leftEdge := (self.space.boundary.x1 )
  68.         local topEdge := (self.space.boundary.y1)
  69.         
  70.         if self.state = @own and n.x < leftEdge then (
  71.             self.state := @losing
  72.             noteLosing self.client
  73.         ) else if self.state = @losing then (
  74.             if (n.x + n.width) < leftEdge then (
  75.                 self.state := @lost
  76.                 noteLost self.client
  77.             ) else if (n.x > leftEdge) then (
  78.                 noteGotBack self.client
  79.             ) else
  80.                 noteStillLosing self.client
  81.         )
  82.     )
  83. )
  84.  
  85. -- End of class definition for Wrapper
  86.